package com.ruoyi.wms.controller;

import java.util.List;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.wms.domain.WmsInventory;
import com.ruoyi.wms.service.IWmsInventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.wms.domain.WmsStockOut;
import com.ruoyi.wms.service.IWmsStockOutService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 出库单管理Controller
 * 
 * @author ruoyi
 * @date 2022-03-17
 */
@RestController
@RequestMapping("/wms/stockOut")
public class WmsStockOutController extends BaseController
{
    @Autowired
    private IWmsStockOutService wmsStockOutService;

    @Autowired
    private IWmsInventoryService wmsInventoryService;

    /**
     * 查询出库单管理列表
     */
    @PreAuthorize(hasPermi = "wms:stockOut:list")
    @GetMapping("/list")
    public TableDataInfo list(WmsStockOut wmsStockOut)
    {
        startPage();
        List<WmsStockOut> list = wmsStockOutService.selectWmsStockOutList(wmsStockOut);
        return getDataTable(list);
    }

    /**
     * 导出出库单管理列表
     */
    @PreAuthorize(hasPermi = "wms:stockOut:export")
    @Log(title = "出库单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsStockOut wmsStockOut) throws IOException
    {
        List<WmsStockOut> list = wmsStockOutService.selectWmsStockOutList(wmsStockOut);
        ExcelUtil<WmsStockOut> util = new ExcelUtil<WmsStockOut>(WmsStockOut.class);
        util.exportExcel(response, list, "出库单管理数据");
    }

    /**
     * 获取出库单管理详细信息
     */
    @PreAuthorize(hasPermi = "wms:stockOut:query")
    @GetMapping(value = "/{stockOutUuid}")
    public AjaxResult getInfo(@PathVariable("stockOutUuid") String stockOutUuid)
    {
        return AjaxResult.success(wmsStockOutService.selectWmsStockOutByStockOutUuid(stockOutUuid));
    }

    /**
     * 新增出库单管理
     */
    @PreAuthorize(hasPermi = "wms:stockOut:add")
    @Log(title = "出库单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsStockOut wmsStockOut)
    {
        wmsStockOut.setCreateBy(SecurityUtils.getUsername());
        return toAjax(wmsStockOutService.insertWmsStockOut(wmsStockOut));
    }

    /**
     * 修改出库单管理
     */
    @PreAuthorize(hasPermi = "wms:stockOut:edit")
    @Log(title = "出库单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsStockOut wmsStockOut)
    {
        return toAjax(wmsStockOutService.updateWmsStockOut(wmsStockOut));
    }

    /**
     * 删除出库单管理
     */
    @PreAuthorize(hasPermi = "wms:stockOut:remove")
    @Log(title = "出库单管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{stockOutUuids}")
    public AjaxResult remove(@PathVariable String[] stockOutUuids)
    {
        return toAjax(wmsStockOutService.deleteWmsStockOutByStockOutUuids(stockOutUuids));
    }



    /**
     * 更新库存
     */
    @Transactional
    @PreAuthorize(hasPermi = "transfer:transfer:remove")
    @Log(title = "调拨单(更新库存)", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/upd-inventory/{uuid}")
    public AjaxResult updInventory(@PathVariable("uuid") String uuid)
    {

        // 取数据
        WmsStockOut so = wmsStockOutService.selectWmsStockOutByStockOutUuid(uuid);

        // 处理记录数（行数）
        AtomicInteger result = new AtomicInteger();

        // 遍历明细
        so.getWmsStockOutDetailsList().forEach(d -> {

            // 减库存
            WmsInventory i = new WmsInventory();
            i.setUpdateAt(DateUtils.getNowDate());
            i.setFkStockUuid(so.getFkStockUuid());
            i.setFkItemUuid(d.getFkItemUuid());
            i.setFkStockPosUuid(d.getFkStockPosUuid());
            i.setInventoryQty(d.getDifferentQty().negate()); // 取负数
            wmsInventoryService.insertWmsInventory(i);

            // 记录处理行数
            result.getAndIncrement();

        });

        // 返回处理的记录数
        return toAjax(result.get());
    }

    /**
     * 更新库存
     */
    @Transactional
    @PreAuthorize(hasPermi = "transfer:transfer:remove")
    @Log(title = "调拨单(更新库存)", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/upd-inventory-loss/{uuid}")
    public AjaxResult updInventoryLoss(@PathVariable("uuid") String uuid)
    {
        // 取数据
        WmsStockOut so = wmsStockOutService.selectWmsStockOutByStockOutUuid(uuid);

        // 处理记录数（行数）
        AtomicInteger result = new AtomicInteger();

        // 遍历明细
        so.getWmsStockOutDetailsList().forEach(d -> {

            // 减库存
            WmsInventory i = new WmsInventory();
            i.setUpdateAt(DateUtils.getNowDate());
            i.setFkStockUuid(so.getFkStockUuid());
            i.setFkItemUuid(d.getFkItemUuid());
            i.setFkStockPosUuid(d.getFkStockPosUuid());
            i.setInventoryQty(d.getDifferentQty()); // 取负数
            wmsInventoryService.insertWmsInventory(i);

            // 记录处理行数
            result.getAndIncrement();

        });

        // 返回处理的记录数
        return toAjax(result.get());
    }


}
